package ua.org.nuos.sdms.clientgui.client.view;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.VerticalLayout;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.*;
import ua.org.nuos.sdms.clientgui.client.handlers.*;
import ua.org.nuos.sdms.clientgui.client.listeners.*;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.User;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 19.02.12
 * Time: 16:55
 * To change this template use File | Settings | File Templates.
 */
public class RootUiHandler extends VerticalLayout implements UserChangedListener {

    private static final Logger logger = Logger.getLogger(RootUiHandler.class.getName());
    public static final String WORKING_ZONE_WIDTH = "960px";
    private static final String HEADER_HEIGHT = "105px";
    private static final String FOOTER_HEIGHT = "40px";

    private VerticalLayout dashboard;
    private GlobalBoard globalBoard;

    public RootUiHandler() {
        initController();
        init();
    }

    private void initController() {
        globalBoard = (GlobalBoard) AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID);
        globalBoard.register(LoginListener.class, LoginEvent.class);
        globalBoard.register(LogoutListener.class, LogoutEvent.class);
        globalBoard.register(UserChangedListener.class, UserChangedEvent.class);
        globalBoard.register(ShowLoginFormListener.class, ShowLoginFormEvent.class);
        globalBoard.register(RegisterNewUserListener.class, RegisterNewUserEvent.class);
        globalBoard.register(ShowRegisterNewUserWindowListener.class, ShowRegisterNewUserWindowEvent.class);
        globalBoard.register(CreateNewGroupListener.class, CreateNewGroupEvent.class);
        globalBoard.register(ShowSelectGroupListener.class, ShowSelectGroupEvent.class);
        globalBoard.register(ChangeCurrentGroupListener.class, ChangeCurrentGroupEvent.class);
        globalBoard.register(SearchGroupListener.class, SearchGroupEvent.class);
        globalBoard.register(BackToSearchGroupListener.class, BackToSearchGroupEvent.class);
        globalBoard.register(AttachUserToGroupListener.class, AttachUserToGroupEvent.class);
        globalBoard.register(UpdateUserListener.class, UpdateUserEvent.class);
        globalBoard.register(UpdateUserPasswordListener.class, UpdateUserPasswordEvent.class);
        globalBoard.register(RemoveUserListener.class, RemoveUserEvent.class);
        globalBoard.register(UpdateGroupListener.class, UpdateGroupEvent.class);
        globalBoard.register(RemoveGroupListener.class, RemoveGroupEvent.class);
        globalBoard.register(ShowRegisterNewUserLayoutListener.class, ShowRegisterNewUserLayoutEvent.class);
        globalBoard.register(DetachCurrentUserFromGroupListener.class, DetachCurrentUserFromGroupEvent.class);
        globalBoard.register(RefuseFromLeaderTypeListener.class, RefuseFromLeaderTypeEvent.class);
        globalBoard.register(DetachUserFromGroupListener.class, DetachUserFromGroupEvent.class);
        globalBoard.register(UpdateUserInGroupToLeaderListener.class, UpdateUserInGroupToLeaderEvent.class);

        globalBoard.addListener(this);
        globalBoard.addListener(new LoginHandler());
        globalBoard.addListener(new LogoutHandler());
        globalBoard.addListener(new ShowLoginFormHandler());
        globalBoard.addListener(new ShowRegisterNewUserWindowHandler());
        globalBoard.addListener(new RegistrationNewUserHandler());
        globalBoard.addListener(new CheckUserLastGroupHandler());
        globalBoard.addListener(new CreateNewGroupHandler());
        globalBoard.addListener(new ShowSelectGroupHandler());
        globalBoard.addListener(new ChangeCurrentGroupHandler());
        globalBoard.addListener(new SearchGroupHandler());
        globalBoard.addListener(new BackToSearchGroupHandler());
        globalBoard.addListener(new AttachUserToGroupHandler());
        globalBoard.addListener(new UpdateUserHandler());
        globalBoard.addListener(new UpdateUserPasswordHandler());
        globalBoard.addListener(new RemoveUserHandler());
        globalBoard.addListener(new UpdateGroupHandler());
        globalBoard.addListener(new RemoveGroupHandler());
        globalBoard.addListener(new ShowRegisterNewUserLayoutHandler());
        globalBoard.addListener(new DetachCurrentUserFromGroupHandler());
        globalBoard.addListener(new RefuseFromLeaderTypeHandler());
        globalBoard.addListener(new DetachUserFromGroupHandler());
        globalBoard.addListener(new UpdateUserInGroupToLeaderHandler());
    }

    private void init() {
        setSizeFull();
        setSpacing(true);

        Header header = new Header();
        globalBoard.addListener(header);
        dashboard = createContentWrapper();

        addComponent(header);
        addComponent(dashboard);

        setComponentAlignment(header, Alignment.TOP_CENTER);
        setComponentAlignment(dashboard, Alignment.MIDDLE_CENTER);

        setExpandRatio(dashboard, 1f);

        refreshDashboard();

        logger.log(Level.INFO, "all components was added");
    }

    private VerticalLayout createContentWrapper() {
        VerticalLayout content = new VerticalLayout();
        content.setWidth(WORKING_ZONE_WIDTH);
        content.setHeight("100%");
        content.setSpacing(false);
        return content;
    }

    private void refreshDashboard() {
        dashboard.removeAllComponents();

        User user = AppProxy.getInstance().getServiceLocator().getAuthenticationBean().getCurrentUser();
        if (user == null) {
            dashboard.addComponent(new HelloLayout());
        } else {
            dashboard.addComponent(new MainLayout());
        }
    }

    @Override
    public void userChanged(UserChangedEvent event) {
        refreshDashboard();
    }
}
